import { defineStore } from 'pinia'
import { createOrder } from '@/api/cart';
import { IarrayCartData } from '@/types/goods'
import { IallAddress ,Iaddress} from '@/types/order'

// 订单
export const useOrderStore = defineStore('order', {
    state: () => {
      return {
        cartData:[] as IarrayCartData,
        allAddressData:[] as IallAddress | [],
        addressData:{} as Iaddress
      }
    },
    getters:{
      // 合计 ： 元 + 00
      countPrice: (state) => {
        let all = 0
        for (const iterator of state.cartData) {
            if(iterator.is_checked === 1){
                all += iterator.goods.price * iterator.num
            }
        }
        return Number(all + '00') // 总价
      }
    },
    actions: {
      async getCreateOrder () {
        let res = await createOrder()
        if(res.status === 200){
          this.cartData = res.data.carts
          this.allAddressData = res.data.address
          // 判断默认地址
          for (const iterator of this.allAddressData) {
            if(iterator.is_default === 1){
              this.addressData = iterator
            }
          }
        }
      }
    }
  })